<template>
  <GoodsList :value="goodsList"
             :addition="addition"
             :total-data="totalData"
             :readonly="readonly"
             :goods-order-type="'order'"
             @input="handleInput" />
</template>

<script>
import _ from "lodash";
import OrderMixin, { STATUS_CODES } from "./order.mixin";
import GoodsList from "@/components/basic/GoodsList.vue";

export default {
  name: "OrderGoodsList",
  components: { GoodsList },
  mixins: [OrderMixin],
  props: {
    isAll: [Boolean, Number]
  },
  data () {
    return {};
  },
  computed: {
    status () {
      return this.order?.status;
    },
    addition () {
      const { volumeAddition: volume, weightAddition: weight } = this.order;
      return { volume, weight, goodsName: "补差" };
    },
    totalData () {
      const { calculateVolume, calculateWeight, totalQuantity: quantity } = this.order;
      return { volume: calculateVolume && calculateVolume.toFixed(2), weight: calculateWeight && calculateWeight.toFixed(2), quantity, goodsName: "合计" };
    },
    goodsReceipt () {
      return ({ receipt = {} }) => {
        if (this.status !== STATUS_CODES.COMPLETED) {
          return {};
        }
        const { quantity: signQuantity, remarks: exceptionDesc, rejects: receiptRejects } = receipt;
        return { ...receipt, signQuantity, exceptionDesc, receiptRejects };
      };
    },
    goodsSign () {
      return (goods) => {
        const goodsSign = goods.sign || {};
        const { quantity: signQuantity, remarks: exceptionDesc, rejects } = goodsSign;
        return { ...goodsSign, signQuantity, exceptionDesc, rejects };
      };
    },
    goodsList () {
      const goodsList = this.order.goodsList?.map((goods) => {
        const { name: goodsName, quantity, remarks: desc } = goods;
        const goodsSign = this.goodsSign(goods);
        const goodsReceipt = this.goodsReceipt(goods);
        const signQuantity = goodsReceipt.quantity || (goodsReceipt.quantity === 0 ? 0 : goodsSign.quantity) || (goodsSign.quantity === 0 ? 0 : quantity);

        let exception = goodsReceipt && goodsReceipt.exceptionQuantity ? goodsReceipt : goodsSign;
        return {
          ...goods,
          ...exception,
          rejects: goodsSign.rejects || [],
          receiptRejects: goodsReceipt.receiptRejects || [],
          goodsName,
          desc,
          quantity,
          goodsSign,
          signQuantity
        };
      });
      return goodsList || [];
    },
    isSignNumOver () {
      return ({ signQuantity, quantity }) => Number(signQuantity) > quantity;
    },
    isSignNumZero () {
      return ({ signQuantity }) => {
        // return !signQuantity && signQuantity !== 0 && signQuantity !== "0";
        return signQuantity === 0 ? false : !signQuantity
      };
    },
    isSignNumLack () {
      return ({ signQuantity, exceptionQuantity, quantity }) => {
        if (Number(exceptionQuantity) !== 0) {
          return Number(signQuantity) + Number(exceptionQuantity) < quantity;
        }
      }

    },
    readonly () {
      return (
        (this.status !== STATUS_CODES.WAIT_SIGN &&
          this.status !== STATUS_CODES.WAIT_RECEIPT) || !!this.isAll
      );
    }
  },
  methods: {
    checkSignNums: _.debounce(function (goods) {
      if (goods.isSignNumOver) {
        this.$message.warning("实签数量不能大于物品数量！");
        return;
      }

      if (goods.isSignNumLack) {
        this.$message.warning("实签数量+异常数量不能小于物品数量！");
        return;
      }

      if (goods.isSignNumZero) {
        this.$message.warning("实签数量不能为空！");
        return;
      }
    }, 600),
    handleInput (goodsList) {
      const nums = goodsList.map((goods) => {
        const formatted = {
          ...goods,
          isSignNumOver: this.isSignNumOver(goods),
          isSignNumZero: this.isSignNumZero(goods),
          isSignNumLack: this.isSignNumLack(goods)
        };
        if (formatted.isSignNumOver || formatted.isSignNumZero || formatted.isSignNumLack) {
          this.checkSignNums(formatted);
        }
        return formatted;
      });
      this.$emit("input", nums);
    }
  }
};
</script>
